import { asyncRoutes } from '@/router/index';

import zh from '@/i18n/lang/zh';
import en from '@/i18n/lang/en';

import config from '@/config';

const { VUE_APP_SYSTEM_PREFIX, VUE_APP_NAME } = config;

export const getMenuFn = () => {
  let routers = [{ moduleName: VUE_APP_NAME, list: asyncRoutes }];

  routers = JSON.parse(JSON.stringify(routers));

  console.log(routers);

  let cArr: any = [];

  const addArr: any = [];

  const allRouterMenu: any = {
    id: 0,
    activeMenu: '',
    leafFlag: 0,
    menuCode: 'dfds',
    menuNameZh: 'dfds',
    menuNameEn: 'dfds',
    menuSort: 0,
    menuType: 0,
    parentId: -1,
    path: '',
    url: '',
    hidden: 1,
    children: [],
  };

  const getRouerArr = (data, parentPath = '') => {
    let pPath = parentPath;

    const arr: any = [];

    data.forEach((item) => {
      const data = {};

      if (item.meta.unAuth || item.meta.isLocal) {
        return;
      }

      if (item.name) {
        const {
          authCode = '',
          title = '',
          button = [],
          activeMenu = '',
          requestUrl = [],
          icon = '',
          moduleName = '',
          menuSort = 0,
          menuType = null,
        } = item.meta;

        const hidden = item.hidden ? 1 : 0;

        let path = item.path;

        // console.log(item.path);

        if (path === '/') {
          const name = item.name;

          path = `/${name.replace(name.charAt(0), name.substring(0, 1).toLowerCase())}`;
        } else {
          if (item.path.substr(0, 1) === '/') {
            path = `${pPath}${path}`;
          } else {
            if (pPath.charAt(pPath.length - 1) === '/') {
              pPath = pPath.substr(0, pPath.length - 1);
            }

            path = `${pPath}/${path}`;
          }
        }

        if (item.redirect) {
          // data['redirect'] = `/${moduleName}${item.redirect}`;

          data['redirect'] = ``;
        }

        data['icon'] = icon;

        data['moduleName'] = moduleName;

        data['menuType'] = menuType !== null ? menuType : item.redirect || item.icon ? 0 : 1;

        data['menuCode'] = `${authCode}`;

        data['path'] = path;

        // data['path'] = '';

        data['menuNameZh'] = zh.route[title] || '';

        data['menuNameEn'] = en.route[title] || '';

        data['children'] = [];

        data['url'] = '';

        data['leafFlag'] = 0;

        data['activeMenu'] = '';

        data['hidden'] = hidden;

        data['parentPath'] = '';

        data['name'] = item.name;

        data['menuSort'] = menuSort;

        // data['parentPath'] = pPath;

        if (requestUrl && requestUrl.length) {
          const requestUrlList: any = [];

          for (const rItem of requestUrl) {
            if (rItem.path) {
              requestUrlList.push(rItem.path as any);
            }
          }

          data['url'] = requestUrlList.join(',');
        }

        if (button && button.length) {
          for (const bItem of button) {
            const bData = {};

            bData['menuType'] = 2;

            bData['path'] = '';

            bData['menuNameZh'] = zh.common.button[bItem.title] || '';

            bData['menuNameEn'] = en.common.button[bItem.title] || '';

            bData['menuCode'] = `${bItem.name}`;

            bData['hidden'] = hidden;

            bData['url'] = bItem.path ? bItem.path : '';

            bData['leafFlag'] = 0;

            bData['menuSort'] = bItem.menuSort || 0;

            data['children'].push(bData);
          }
        }

        if (item.children) {
          data['children'] = data['children'].concat(getRouerArr(item.children, `${path}/`));
        }

        if (activeMenu) {
          if (pPath.charAt(pPath.length - 1) === '/') {
            pPath = pPath.slice(0, pPath.length - 1);
          }

          // data['pPath'] = pPath;

          data['activeMenu'] = `${activeMenu}`;

          cArr.push(data);
        } else {
          arr.push(data);
        }
      }
    });

    return arr;
  };

  const resetMenuFn = (cItem, data) => {
    data.forEach((item) => {
      if (item.menuType !== '2') {
        // if (item.path.includes('productionManger')) {
        //   console.log(item.path);
        //   console.log(cItem.activeMenu);
        // }

        // console.log(cItem);

        if (item.name === cItem.activeMenu) {
          // cItem.activeMenu = `${cItem.pPath}`;

          // delete cItem.pPath;

          if (!item.children) {
            item.children = [];
          }

          item.children.push(cItem);

          // return false;
        } else {
          if (item.children) {
            resetMenuFn(cItem, item.children);
          }
        }
      }
    });
  };

  // const menuList = [];

  // console.log(resetMenuFn(getRouerArr(routers[2]), cArr));

  for (const item of routers) {
    const { moduleName = 'main', list } = item;

    // console.log(moduleName);

    const parent: any = {
      activeMenu: '',
      hidden: 0,
      icon: 'tab',
      leafFlag: 0,
      menuCode: `${VUE_APP_SYSTEM_PREFIX}-` + moduleName,
      menuNameZh: zh.route.app,
      menuNameEn: en.route.app,
      menuSort: 0,
      menuType: 0,
      moduleName: moduleName,
      parentPath: '',
      path: '',
      redirect: '',
      url: '',
    };

    if (list.length) {
      cArr = [];

      const arr = getRouerArr(list, `/${VUE_APP_NAME}`);

      // console.log(cArr);

      for (const cItem of cArr) {
        resetMenuFn(cItem, arr);
      }

      // const [parent, ...child] = arr;

      parent.children = arr;

      allRouterMenu.children.push(parent as any);
    }
  }

  const setRouteIdFn = (list = [], parentId = 0, level = 0) => {
    const arr: any = [];

    list.forEach((item: any, key) => {
      const childAddNum = addArr[level] || 0;

      let levelNum = '10';

      const data: any = {
        ...item,
      };

      if (level !== 0) {
        for (let i = 0; i < level; i++) {
          levelNum += '00';
        }
      }

      addArr[level] = childAddNum + 1;

      const id = addArr[level] + Number(levelNum);

      data['id'] = id;

      data['parentId'] = parentId;

      data['menuSort'] = item.menuSort ?? key;

      if (item.children && item.children.length) {
        data['children'] = setRouteIdFn(item.children, id, level + 1);
      }

      arr.push(data as any);
    });

    return arr;
  };

  allRouterMenu.children = setRouteIdFn(allRouterMenu.children);

  return allRouterMenu;
};

export default getMenuFn();
